﻿using System;
using SmartSiteManagementSystem.IRepository.Admin;
using SmartSiteManagementSystem.Models.Admin;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.Admin
{
    public class AdminGroupBtnRelInfoRepository : IAdminGroupBtnRelInfoRepository
    //,IDisposable
    {
        #region dataTable
        /*
create table `admin_group_btnrel`(
	`rel_id` int not null auto_increment primary key,
	`admin_user_groupid` int not null,
	`btn_id` int not null,
	`admin_menuid` int not null
    )default charset=utf8;
         */
        #endregion
       // private IDbConnection _readConn;
       // private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            throw new NotImplementedException();
        }

        public AdminGroupBtnRelInfo Get(Hashtable param, int IsReadonly = 0)
        {
            throw new NotImplementedException();
        }

        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(rel_id) from admin_group_btnrel";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("rel_id"))
                    {
                        pars.Add("rel_id", param["rel_id"]);
                        whereCondition.Append("and rel_id =@rel_id");
                    }
                    if (param.ContainsKey("admin_user_groupid"))
                    {
                        pars.Add("admin_user_groupid", param["admin_user_groupid"]);
                        whereCondition.Append(" and admin_user_groupid =@admin_user_groupid");
                    }
                    if (param.ContainsKey("btn_id"))
                    {
                        pars.Add("btn_id", param["btn_id"]);
                        whereCondition.Append(" and btn_id =@btn_id");
                    }
                    if (param.ContainsKey("admin_menuid"))
                    {
                        pars.Add("admin_menuid", param["admin_menuid"]);
                        whereCondition.Append(" and admin_menuid =@admin_menuid");
                    }
                }

                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        public IList<AdminGroupBtnRelInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {
            IList<AdminGroupBtnRelInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT rel_id as RelId,admin_user_groupid as AdminUserGroupId,btn_id as BtnId,admin_menuid as AdminMenuId from admin_group_btnrel";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("rel_id"))
                    {
                        pars.Add("rel_id", param["rel_id"]);
                        whereCondition.Append("and rel_id =@rel_id");
                    }
                    if (param.ContainsKey("admin_user_groupid"))
                    {
                        pars.Add("admin_user_groupid", param["admin_user_groupid"]);
                        whereCondition.Append(" and admin_user_groupid =@admin_user_groupid");
                    }
                    if (param.ContainsKey("btn_id"))
                    {
                        pars.Add("btn_id", param["btn_id"]);
                        whereCondition.Append(" and btn_id =@btn_id");
                    }
                    if (param.ContainsKey("admin_menuid"))
                    {
                        pars.Add("admin_menuid", param["admin_menuid"]);
                        whereCondition.Append(" and admin_menuid =@admin_menuid");
                    }

                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by rel_id asc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<AdminGroupBtnRelInfo>(execSql, pars).AsList<AdminGroupBtnRelInfo>();
            }
            return back;
        }

        public int Insert(AdminGroupBtnRelInfo obj)
        {
            throw new NotImplementedException();
        }

        public bool Update(AdminGroupBtnRelInfo obj)
        {
            throw new NotImplementedException();
        }
    }
}

